<template>
    <div>
      <el-form :model="formInline" :inline="true" class="demo-form-inline">
        <el-form-item label="单号">
          <el-input v-model="formInline.number" placeholder="请输入单据编号" clearable />
        </el-form-item>
        <el-form-item label="商品信息：">
          <el-input v-model="formInline.supName" placeholder="请输入商品信息" clearable />
        </el-form-item>
        <el-form-item label="单据日期">
          <el-date-picker v-model="formInline.date" type="datetimerange" start-placeholder="开始时间" end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss" date-format="YYYY/MM/DD ddd" time-format="A hh:mm:ss"
            style="width: 280px; margin-left: 10px;" />
        </el-form-item>
  
        <el-form-item>
          <el-button type="primary" @click="loadMenus">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-table :data="page.records" ref="table" style="width: 1450px">
        <el-table-column type="selection" width="50" />
        <el-table-column prop="erpSupplier.supName" label="客户" width="140" />
        <el-table-column prop="number" label="单据编号" width="170" />
        <el-table-column prop="erpDepotItemList[0].erpMaterial.material_name" label="商品信息" width="320" />
        <el-table-column prop="createTime" label="单据日期" width="170" />
        <el-table-column prop="erpUser.username" label="操作员" width="84" />
        <el-table-column prop="erpDepotItemList[0].operNumber" label="数量" width="82" />
        <el-table-column prop="erpDepotItemList[0].allPrice" label="金额合计" width="82" />
        <el-table-column prop="erpDepotItemList[0].taxLastMoney" label="含税金额" width="82" />
        <el-table-column label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.status == 0" type="danger" effect="light">未审核</el-tag>
            <el-tag v-else-if="scope.row.status == 1" type="success" effect="light">已审核</el-tag>
            <el-tag v-else-if="scope.row.status == 2" type="warning" effect="light">完成销售</el-tag>
            <el-tag v-else-if="scope.row.status == 3" type="warning" effect="light">
              部分销售
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
  
      <el-pagination v-model:page-size="page.size" v-model:current-page="page.current"
        @current-change="handleCurrentChange" @size-change="handleSizeChange" :page-sizes="[2, 4, 8, 10]"
        layout="total, sizes, prev, pager, next, jumper" :total="page.total" background style="margin-top:20px;">
      </el-pagination>
  
      <el-button type="primary"
        @click="saveSelected">保存</el-button>
  
      <el-dialog :destroy-on-close="true" :title="title" v-model="visible" width="1550px" height="500px"
        style="margin-left: 250px;">
        <router-view @close="close" @iii="iii" :data="data"></router-view>
      </el-dialog>
    </div>
  </template>

  
  <script setup>
  import { reactive, ref, defineEmits, defineProps } from 'vue';
  import request from '@/api/request';
  import { ElMessage } from 'element-plus';
  import router from '@/router';
  
  const visible = ref(false);
  const title = ref("");
  const prop = defineProps(['supId'])
  let emit = defineEmits(['saveSaleinorder', 'ert', 'proty','supName']);
  const data = ref([]);
  const table = ref(null)
  console.log(prop.supId)
  
  const page = ref({
    current: 1,
    size: 10,
    total: 0,
    records: []
  });
  
  const close = () => {
    visible.value = false;
  };
  
  
  const handleCurrentChange = (val) => {
    page.value.current = val;
    loadMenus();
  };

  const supName = (val) => {

    console.log(val);
  };
  const handleSizeChange = (val) => {
    page.value.size = val;
    page.value.current = 1;
    loadMenus();
  };
  
  const formInline = reactive({
    number: '',
    supName: '',
    date: ''
  });
  
  const loadMenus = () => {
    request({
      url: '/sale/findByPageAssociateorder2',
      method: "get",
      params: {
        current: page.value.current,
        size: page.value.size,
        number: formInline.number,
        subType:'销售订单',
        organId: prop.supId,
      }
    }).then(res => {
      page.value.records = res.data.records;
      page.value.total = res.data.total;
      console.log(res);
    }).catch(err => {
      ElMessage.error('加载数据失败');
    });
  };
  

  
  const selectedItems = ref([])
  const iii = (item) => {
    console.log(item);
    emit('ert', item);
  
  }
  
  
  
  
  
  const saveSelected = () => {
    let tableData = table.value.getSelectionRows()
    console.log(tableData);


    emit('table', tableData);
 
  
  
  };
  
  const handleSelectionChange = (val) => {
  selectedItems.value = val;
  emit('saveSaleinorder', selectedItems.value);
  console.log(selectedItems.value);
};
  
  const resetForm = () => {
    formInline.number = '';
    formInline.supName = '';
    formInline.date = '';
  };
  
  // 初始加载数据  
  loadMenus();  
  </script>
  